var baseUrl = "http://doodle4googleau.googlecode.com/svn/trunk/mapplet/";
var slideshowUrl = baseUrl + "d4gplayer.html";
var markerUrl = baseUrl + "pens.png";
var kmlUrl = baseUrl + "kml/";

var midPoint = ({
"ACT":	[149.0341829980641,-35.35885576153097,10],
"NSW":  [151.0034510000001,-33.81695700000002,8],
"QLD":	[144.1410470916779,-22.71781070929745,6],
"NT":   [130.8418850642808,-12.46125079862938,10],
"WA":   [115.8585900000001,-31.95540000000001,8],
"SA":   [138.599732,-34.92577,10],
"VIC":	[144.2858394579669,-36.81595246560855,8],
"TAS":	[147.3281251091405,-42.8491669555194,10]
});

function loadKml() {
	map.setCenter(new GLatLng(-27.215556, 134.648438), 4);
	/*for (var state in midPoint) {
		map.addOverlay(new GGeoXml(kmlUrl + state + '.kml'));		
	}*/
}

function plotLocation(idschool, stateMap, point) {
	var tinyIcon = new GIcon();
	tinyIcon.image = markerUrl;
	//tinyIcon.iconSize = new GSize(12, 20);
	tinyIcon.iconAnchor = new GPoint(6, 20);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
	
	var marker;
	var html = [];
	var tabNames = [];
	marker = new GMarker(point, {icon:tinyIcon});
	var snlist = "";
	var yllist = "";
	for (var i = 0; i < stateMap[idschool]["idd"].length; i++) {
		var doodleId = stateMap[idschool]["idd"][i];
		snlist += doodleMap[doodleId]["sn"] + ",";
		yllist += doodleMap[doodleId]["yl"] + ",";
	}
	var didlist = stateMap[idschool]["idd"].join(',');
	var iframeSrc = slideshowUrl + '?' + 'sid=' + escape(idschool) + '&sc=' + escape(stateMap[idschool]["name"]) + '&did=' + escape(didlist) + '&sn=' + escape(snlist) + '&yl=' + escape(yllist);
	var str = '<div style=\'width:310px;height:340;\' class=\'placemark\'>' + '<iframe src=\'' + iframeSrc + '\' width=\'100%\' height=\'350px\' scrolling=\'auto\' frameborder=\'0\'></iframe></div>';
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(str);
	});
	map.addOverlay(marker);
	return marker;
}

function renderMap(idschool) {
	if (idschool == "")
		return;
	var selectedState = document.getElementById('stateMenu').value;
	var stateMap = schoolMap[selectedState];
	var pt = new GLatLng(stateMap[idschool]["lat"], stateMap[idschool]["lng"]);	
	map.clearOverlays(); 
	var marker = plotLocation(idschool, stateMap, pt);
	map.setCenter(pt, 8);
	GEvent.trigger(marker, "click");
}

function showAll(opt) {
	if (opt == false) 
		return;
	var selectedState = document.getElementById('stateMenu').value;
	var stateMap = schoolMap[selectedState];
	map.clearOverlays(); 
	map.setCenter(new GLatLng(midPoint[selectedState][1], midPoint[selectedState][0]), midPoint[selectedState][2]);
	map.addOverlay(new GGeoXml(kmlUrl + selectedState + '.kml'));		
}

function populateList() {
	var selectedState = document.getElementById('stateMenu').value;
	var html = [];
	html.push('<select id="school" size=1 onchange="renderMap(this.value);" class="mainForm">');	
	if (selectedState != '') {
		var stateMap = schoolMap[selectedState];
		html.push('<option value=\'\'> Please select a school</option>');
		for (var schoolId in stateMap) {
			var schoolName = stateMap[schoolId]["name"];
			html.push('<option value=\'' + schoolId + '\' id=\'list-' + schoolId + '\'>' + schoolName + '</option>');
		}
	}
	html.push('</select>');
	var listElement = document.getElementById('list');
	listElement.innerHTML = html.join('');
	if (listElement.value == '') {
		listElement.disabled = true;
	}
	showAll(true);
}
